<template>
    <div class="main">
      <div v-if="userAll">
        <el-table :data="list" v-loading="queryLoading" style="width: 100%;
      height: 79vh;" :default-sort="{ prop: 'date', order: 'descending' }" key="table1">
          <el-table-column type="selection" width="55px" v-if="checkBox">
          </el-table-column>
          <el-table-column label="ID" prop="id" key="id">
            <template slot-scope="scope">
              <span style="">{{ scope.row.id }}</span>
            </template>
          </el-table-column>
          <el-table-column label="菜品名字" prop="name" align="center">
            <template slot-scope="scope">
              <span style="">{{ scope.row.name }}</span>
            </template>
          </el-table-column>
          <el-table-column label="分类名称" prop="categoryId" sortable align="center">
            <template slot-scope="scope">
              <span style="">{{ scope.row.categoryName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="价格" prop="price" align="center">
            <template slot-scope="scope">
              <span style="">{{ scope.row.price }}￥</span>
            </template>
          </el-table-column>
          <el-table-column label="图片" prop="image" sortable align="center">
            <template slot-scope="scope">
              <div style="width: 100px;height: 50px;">
                <el-image :src="scope.row.image" lazy></el-image>
              </div>
              <!-- <span style="">{{ scope.row.image }}</span> -->
            </template>
          </el-table-column>
          <el-table-column label="信息" prop="description" align="center">
            <template slot-scope="scope">
              <span style="">{{ scope.row.description }}</span>
            </template>
          </el-table-column>
          <el-table-column label="状态" prop="status" sortable align="center" width="190px">
            <template slot-scope="scope">
              <span style=""> {{ scope.row.status ===1?"起售":"停售"}}</span>
              <!-- <el-switch v-model="scope.row.status" active-value="1" inactive-value="0" active-text="起售" inactive-text="停售"
                active-color="#13ce66" inactive-color="#ff4949" @change="dishStatus" ref="status">
              </el-switch> -->
            </template>
          </el-table-column>
          <el-table-column align="center" width="150px" label="操作">
            <template slot-scope="scope">
              <el-button size="medium" type="primary" @click="handleAdd(scope.$index, scope.row)"
                style="width: 60px;height: 40px;">加入购物车</el-button>
              <!-- <el-button size="medium" type="danger" @click="handleDelete(scope.$index, scope.row)"
                style="width: 60px;height: 40px;">移除购物车</el-button> -->
            </template>
          </el-table-column>
        </el-table>
      </div>
      <userEditView :dialogVisible.sync="dialogVisible" :dialogInfo.sync="dialogInfo"></userEditView>
      <userDeleteView :dialogVisible_delete.sync="dialogVisible_delete" :dialogInfo_delete.sync="dialogInfo_delete">
      </userDeleteView>
      <el-pagination :total="pagination.total" :current-page.sync="pagination.currentPage"
        :page-size.sync="pagination.PageSize" :page-sizes="[6]" layout="total, sizes, prev, pager, next, jumper"
        @size-change="query()" @current-change="query()" />
    </div>
  </template>
<script>
import { METHOD, request } from '@/utils/request'
import { Message } from 'element-ui'
// import Axios from 'axios'
export default {
  name: 'userDishView',
  components: {
  },
  created () {
    this.query()
  },
  watch: {
    dialogInfo: {
      handler (newVal, oldVal) {
        const params = JSON.stringify(newVal)
        console.log(params)
        this.Editquery(params)
        // this.query()
      }
    }
  },
  // updated () {
  //   this.query()
  // },
  computed () {
    console.log(this.input)
  },
  mounted () {
  },
  data () {
    return {
      userAll: true,
      list: {},
      queryLoading: true, // 查询加载状态
      pagination: {
        // 分页数据
        total: 0, // 记录总数
        currentPage: 1, // 当前页
        PageSize: 6, // 每页的记录条数
        sort: 'updateAt,desc' // 查询排序依据
      }
    }
  },
  methods: {
    change () {
      this.$forceUpdate()
    },
    query () {
      this.userAll = true
      this.queryLoading = true
      this.userSearch = false
      const params = Object.assign(
        {
          page: this.pagination.currentPage,
          pageSize: this.pagination.PageSize
        })
      request('/api/user/dish/list', METHOD.POST, params).then((resp) => {
        this.list = resp.data.data.records
        this.pagination.total = resp.data.data.total
        this.currentPage = resp.data.data.currentPage
        this.pageSize = resp.data.data.pageSize
        this.queryLoading = false
      })
    },
    handleAdd (index, row) {
      const params = { dishId: row.id }
      console.log(row)
      request('/api/user/shoppingCart/add', METHOD.POST, params).then((resp) => {
        if (resp.status === 200) {
          Message({
            message: '加入购物车成功!',
            type: 'success',
            duration: 5 * 1000
          })
        }
      })
    },
    // handleDelete (x, y) {
    //   const params = { dishId: y.id }
    //   request('/api/user/shoppingCart/sub', METHOD.POST, params).then((resp) => {
    //     if (resp.status === 200) {
    //       Message({
    //         message: '删除菜品成功!',
    //         type: 'success',
    //         duration: 5 * 1000
    //       })
    //     }
    //   })
    // },
    dishStatus () {
      console.log(this.$refs.status.value)
    },
    categoryIdSearch () {

    }
  }
}
</script>
  <style scoped>
  * {
    margin: 0;
    padding: 0;
  }

  .el-pagination {
    margin-left: 25%;
    width: 200px;
    height: 50px;
  }
  </style>
